<?php
namespace Tbf\Bundle\TbfBundle\DependencyInjection;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerAware as BaseContainerAware;
use Symfony\Component\DependencyInjection\ScopeInterface;
use Tbf\Component\Arrays\ArrayFunction;

/**
 * 这个类表示一个包含container的类,
 * 提供了一些快捷方法
 * 继承Controller可以让phpstorm的symfony2插件进行类型和代码提示,缺点:引入大量无效函数
 * 备注: 使用controller下列函数:get has getDoctrine
 */
class TbfContainerAware extends Controller // implements ContainerGetterInterface
{

    /**
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
     */
    protected function getContainer()
    {
        return $this->container;
    }


    protected function getParameter($name)
    {
        return $this->container->getParameter($name);
    }

    /**
     * create a dbal query Builder
     * @return \Doctrine\DBAL\Query\QueryBuilder
     */
    protected function createDDQB()
    {
        return $this->getDbalConnection()->createQueryBuilder();
    }

    /**
     * @return \Doctrine\DBAL\Connection
     */
    protected function getDbalConnection()
    {
        return $this->container->get('database_connection');
    }

    /**
     * @return \Doctrine\ORM\EntityManager
     */
    protected function getDM()
    {
        return $this->getDoctrine()->getManager();
    }
    /**
     * doctrine repository
     *
     * @param string $name entity name #entity
     *
     * @return \Doctrine\ORM\EntityRepository
     */
    protected function getDR($name)
    {
        return $this->getDM()->getRepository($name);
    }

    /**
     * @return \Doctrine\ORM\QueryBuilder
     */
    protected function createOrmQB(){
        return $this->getDoctrine()->getManager()->createQueryBuilder();
    }
    /**
     * @return bool
     */
    protected function isSymfonyGranted($attributes, $object = null)
    {
        return $this->get('security.context')->isGranted($attributes,$object);
    }
    protected function getFromGet($name)
    {
        return $this->getRequest()->query->get($name);
    }
    protected function getFromPost($name)
    {
        return $this->getRequest()->request->get($name);
    }
    protected function  getFromSession($name)
    {
        return $this->get('session')->get($name);
    }
    protected function getFromSession2Level($k1,$k2){
        $d1 = $this->getFromSession($k1);
        if (empty($d1)){
            return null;
        }
        if (!is_array($d1)){
            throw new \Exception('getFromSession2Level fail because of base key not an array');
        }
        if (!array_key_exists($k2,$d1)){
            return null;
        }
        return $d1[$k2];
    }
    protected  function setToSession($key,$value)
    {
        $this->get('session')->set($key,$value);
    }
    protected function setToSession2Level($k1,$k2,$value){
        $d1 = $this->getFromSession($k1);
        if (empty($d1)){
            $d1 = array();
        }
        if (!is_array($d1)){
            throw new \Exception('getFromSession2Level fail because of base key not an array');
        }
        $d1[$k2] = $value;
        $this->setToSession($k1,$d1);
        return;
    }
    /**
     * validator
     * @return \Symfony\Component\Validator\Validator
     */
    protected function getValidator()
    {
        return $this->container->get('validator');
    }

    /**
     * 检查里面的所有的字段都存在
     * @param  map[string]interface{} $data
     * @param  []string               $require_list
     * @return null|string            err
     */
    protected function checkParameterExist($data,array $require_list)
    {
        return ArrayFunction::checkParameterExist($data,$require_list);
    }

    /**
     * 过滤字段
     * @param map[string]interface{} $data
     * @param $allow_list
     * @return map[string]interface{}
     */
    protected function filterParameter($data,array $allow_list)
    {
        return ArrayFunction::filterParameter($data,$allow_list);
    }

    /**
     * 查询数组的一个键的值,如果不存在,则返回默认值
     * @param $data
     * @param $key
     * @param $default
     */
    protected function defaultParameter($data,$key,$default=array())
    {
        return ArrayFunction::defaultParameter($data,$key,$default);
    }
    protected function toArray($obj,$group=null)
    {
        return $this->get('tbf_serializer')->toArray($obj,$group);
    }
    protected function loadFromArray($obj,$input,$group=null)
    {
        return $this->get('tbf_serializer')->loadFromArray($obj,$input,$group);
    }
    protected function logInfo($message,$context=array()){
        return $this->get('logger')->addInfo($message,$context);
    }
    protected function logError($message,$context=array()){
        return $this->get('logger')->addError($message,$context);
    }
    protected function logDebug($message,$context=array()){
        return $this->get('logger')->addDebug($message,$context);
    }
}
